package com.me.mygdxgame;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.me.mygdxgame.Components.Attack;
import com.me.mygdxgame.Components.TilebasedCharacterController;
import com.me.mygdxgame.GameObjects.Tile;
import com.me.mygdxgame.GameObjects.Unit;

public class RoundHandler {
	private Unit selectedUnit;
	private Tile selectedTile;
	private int ap;
	GAMESTATE gamestate = GAMESTATE.PLACEMENT;
	boolean playerOnesTurn = false;
	private static RoundHandler instance = null;
	
	public static RoundHandler getInstance(){
		if(instance == null){
			instance = new RoundHandler();
		}
		return instance;
	}
	
	public void switchPlayer(){
		if(playerOnesTurn){
			playerOnesTurn = false;
			return;
		}
		else{
			playerOnesTurn = true;
		}
	}
	
	public void handleActions(){
		if(gamestate == GAMESTATE.PLACEMENT){
			Map.placement();
		}
		if(gamestate == GAMESTATE.SELECTION){
			selectedUnit = getSelectedUnit();
		}
		if(gamestate == GAMESTATE.MOVEMENT){
			handleMovement();
		}
		if(gamestate == GAMESTATE.ATTACK){
			handleAttack();
		}
	}
	
	void handleAttack(){
		Tile t = selectedUnit.getOccupiedTile();
		ArrayList<Unit> unitsToAttack = new ArrayList<Unit>();
		for(Tile ti:t.getAttackArea(selectedUnit.getUnittype(), DIRECTION.UP)){
			unitsToAttack.add(ti.getOccupyUnit());
		}
		Attack attack = (Attack)selectedUnit.getComponent("Attack");
		attack.attack(unitsToAttack);
		setNextState();
	}
	
	void setNextState(){
		ap--;
		gamestate = GAMESTATE.SELECTION;
		selectedUnit = null;
		selectedTile = null;
		if(ap <= 0){
			switchPlayer();
			ap = 2;
		}
	}
	
	void handleMovement(){
		selectedTile = getMovementTargetTile();
		if(selectedTile != null){
			performMovement();
		}
	}
	void performMovement(){
		TilebasedCharacterController controller = (TilebasedCharacterController)selectedUnit.getComponent("TilebasedCharacterController");
		controller.move(selectedUnit.getOccupiedTile(), selectedTile);
		com.me.mygdxgame.Components.Transform tr = (com.me.mygdxgame.Components.Transform)selectedTile.getComponent("Transform");
		if(controller.checkIfTargetReached(tr.getPosition())){
			selectedUnit.setOccupiedTile(selectedTile);
			setNextState();
		}
	}
	
	Tile getMovementTargetTile(){
		for(Tile currentTile:selectedUnit.getOccupiedTile().getTilesInMovementRange(TabletWars.movementRange)){
			com.me.mygdxgame.Components.BoundingBox box = (com.me.mygdxgame.Components.BoundingBox)currentTile.getComponent("BoundingBox");
			box.touchFunction(Gdx.input.getX(), Gdx.input.getY());
			if(box.touched()){
				if(Gdx.input.justTouched()){
					if(selectedUnit != null){
						return currentTile;
					}
				}
			}
		}
		return null;
	}
	
	Unit getSelectedUnit(){
		ArrayList<Unit> unitsToCheck = new ArrayList<Unit>();
		if(playerOnesTurn){
			unitsToCheck = TabletWars.player1Units;
		}
		else{
			unitsToCheck = TabletWars.player2Units;
		}
		for(Unit unit:unitsToCheck){
			com.me.mygdxgame.Components.BoundingBox box = (com.me.mygdxgame.Components.BoundingBox)unit.getComponent("BoundingBox");
			box.touchFunction(Gdx.input.getX(), Gdx.input.getY());
			if(box.touched()){
				if(Gdx.input.justTouched()){
					return unit;
				}
			}
		}
		return null;
	}
}
